package cn.org.intelli.lims.o2oa.service.impl;

import cn.org.intelli.lims.o2oa.common.RestResult;
import cn.org.intelli.lims.o2oa.service.O2oaService;
import cn.org.intelli.lims.o2oa.util.Uid;
import cn.org.intelli.lims.o2oa.util.WordToHtmlUtil;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class O2oaServiceImpl implements O2oaService {

    @Value("${lims-plus.o2oa.recorder-path}")
    private String recorderPath;
    @Value("${lims-plus.o2oa.recorder-nginx-url}")
    private String nginxUrl;

    @Override
    public RestResult<Object> generateDocReport(String inspection, String clientDepartment, String transferInspectPerson, String caseSerialNum, String acceptSerialNum, String entrustRequire, String briefCaseInfo, String orginAutDeptAndResult, String experimenter, String equipmentSerialNum, String equipmentName, String typeAndAmout, String stroageStatus, String place, String equipment, String consumableSerialNum, String consumableName, String consumableFactory, String performanceParameter, String methodSerialNum, String standardSerialNum, String processDescription, String inspectionData, String authenticateOpinion, String inspectPerson, String certigier) {
        Date curDateTime = new Date(System.currentTimeMillis());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String curTime = format.format(curDateTime);
        System.out.println(curTime);
        Map<String, Object> resMap = new HashMap<>();
        String fileName = "文档" + ".docx";
        File reportStorageFile = new File(recorderPath);
        if (!reportStorageFile.exists()) {
            reportStorageFile.mkdirs();
        }
        String finalOutputFile = recorderPath + File.separator + fileName;
        String xml2DocPath = "";
        String htmlPath = "";
        String uid = "";
        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("inspection", inspection);
            dataMap.put("clientDepartment", clientDepartment);
            dataMap.put("transferInspectPerson", transferInspectPerson);
            dataMap.put("caseSerialNum", caseSerialNum);
            dataMap.put("acceptSerialNum", acceptSerialNum);
            dataMap.put("entrustRequire", entrustRequire);
            dataMap.put("briefCaseInfo", briefCaseInfo);
            dataMap.put("orginAutDeptAndResult", orginAutDeptAndResult);
            dataMap.put("experimenter", experimenter);
            dataMap.put("equipmentSerialNum", equipmentSerialNum);
            dataMap.put("equipmentName", equipmentName);
            dataMap.put("typeAndAmout", typeAndAmout);
            dataMap.put("stroageStatus", stroageStatus);
            dataMap.put("place", place);
            dataMap.put("equipment", equipment);
            dataMap.put("consumableSerialNum", consumableSerialNum);
            dataMap.put("consumableName", consumableName);
            dataMap.put("consumableFactory", consumableFactory);
            dataMap.put("performanceParameter", performanceParameter);
            dataMap.put("methodSerialNum", methodSerialNum);
            dataMap.put("standardSerialNum", standardSerialNum);
            dataMap.put("processDescription", processDescription);
            dataMap.put("inspectionData", inspectionData);
            dataMap.put("authenticateOpinion", authenticateOpinion);
            dataMap.put("inspectPerson", inspectPerson);
            dataMap.put("certigier", certigier);
            dataMap.put("inspectionTime", curTime.substring(0, 16));
            Configuration configuration = new Configuration(new Version("2.3.0"));
            configuration.setDefaultEncoding("utf-8");

            //服务器放置模板的位置
            ClassPathResource classPathResource = new ClassPathResource("tpl/");
            File file = classPathResource.getFile();
            String resourceFile = file.getPath();
            configuration.setDirectoryForTemplateLoading(new File(resourceFile));
            Template template = configuration.getTemplate("record.ftl", "utf-8");

            //输出文档路径及名称
            File outFile = new File(finalOutputFile);
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),
                    "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
            uid = Uid.generateShortUuid();
            //String xml2DocPath = "E:\\test\\实验室系统0\\"+"实验记录文档"+".docx";
            xml2DocPath = recorderPath + "/" + uid + ".docx";
            htmlPath = recorderPath + "/" + uid + ".html";
            File xmlToDoc = new File(xml2DocPath);
            FileOutputStream outDoc = new FileOutputStream(xmlToDoc);
            try {
                Document document = new Document(finalOutputFile);
                document.save(outDoc, SaveFormat.DOCX);
                outDoc.close();
                resMap.put("resHtmlPath", xml2DocPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
            WordToHtmlUtil.WordAutoToHtml(new File(xml2DocPath), "image_", "image/");
            //Doc2HtmlUtil.docx(outputFilePath,"文档.docx","html测试.html","","");
        } catch (IOException | TemplateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return RestResult.sucess(nginxUrl + "/" + uid + ".html");
    }
}
